package com.xinongbest.mall.controller;

import com.github.pagehelper.PageInfo;
import com.xinongbest.mall.common.ApiRestResponse;
import com.xinongbest.mall.exception.ImoocMallException;
import com.xinongbest.mall.exception.ImoocMallExceptionEnum;
import com.xinongbest.mall.model.dto.ProductAddDto;
import com.xinongbest.mall.service.ProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
public class ProductAdminController {

    @Autowired
    ProductService productService;

    @PostMapping("/admin/product/add")
    public ApiRestResponse addProduct(@Valid @RequestBody ProductAddDto productAddDto) {
        productService.add(productAddDto);
        return ApiRestResponse.success();
    }

    @PostMapping("/admin/upload/file")
    public ApiRestResponse uploadImage(HttpServletRequest servletRequest, @RequestParam("file") MultipartFile uploadFile) {
        String uploadPath = productService.uploadImage(servletRequest, uploadFile);
        if (StringUtils.isEmpty(uploadPath)) {
            throw new ImoocMallException(ImoocMallExceptionEnum.UPLOAD_FILE_FIELD);
        }
        return ApiRestResponse.success(uploadPath);
    }

    @ApiOperation("管理员操作-批量上下架商品接口")
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public ApiRestResponse batchUpdateSellStatus(@RequestParam Integer[] ids, @RequestParam Integer sellStatus) {
        productService.batchUpdateSellStatus(ids, sellStatus);
        return ApiRestResponse.success();
    }

    @ApiOperation("管理员操作-商品列表")
    @PostMapping("/admin/product/list")
    public ApiRestResponse list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo list = productService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(list);
    }
}
